//
//  UIView+receivedKeyboard.m
//  BadBoy
//
//  Created by wen on 2017/8/28.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "UIView+receivedKeyboard.h"
#import <YYTextKeyboardManager.h>
#import <objc/runtime.h>

static char *EndWindowEditingWhenTouchString = "EndWindowEditingWhenTouchString";

@implementation UIView (receivedKeyboard)

+ (void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method oldMethod = class_getInstanceMethod(self, @selector(init));
        Method newMethod = class_getInstanceMethod(self, @selector(touchNew_init));
        method_exchangeImplementations(oldMethod, newMethod);
    });
}

- (instancetype)touchNew_init{
    UIView *ob = [self touchNew_init];
    ob.endWindowEditingWhenTouch = YES;
    return ob;
}

- (void)setEndWindowEditingWhenTouch:(BOOL)endWindowEditingWhenTouch{
    objc_setAssociatedObject(self, EndWindowEditingWhenTouchString, @(endWindowEditingWhenTouch), OBJC_ASSOCIATION_ASSIGN);
}

- (BOOL)endWindowEditingWhenTouch{
    NSNumber *t = objc_getAssociatedObject(self,EndWindowEditingWhenTouchString);
    return (BOOL)[t boolValue];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    if (self.endWindowEditingWhenTouch == NO) {
        return;
    }
    NSString *classString = NSStringFromClass(self.class);
    NSString *superClassString = NSStringFromClass(super.class);
    NSString *superViewClassString = NSStringFromClass(self.superview.class);
    if ([classString containsString:@"UIKeyboard"] || [superClassString containsString:@"UIKeyboard"] || [superViewClassString containsString:@"UIKeyboard"]) {
        return;
    }
    
    if ([self isKindOfClass:[UITextView class]] || [self isKindOfClass:[UITextField class]] || [self isKindOfClass:[YYTextContainerView class]]) {
        return;
    }
    UITouch *touch = touches.anyObject;
    CGPoint point = [touch locationInView:[UIApplication sharedApplication].keyWindow];
    YYTextKeyboardManager *manager = [YYTextKeyboardManager defaultManager];
    
    // 获取键盘当前状态
    BOOL visible = manager.keyboardVisible;
    CGRect frame = manager.keyboardFrame;
    
    if (CGRectContainsPoint(frame, point) == NO && visible == YES) {
        [[UIApplication sharedApplication].keyWindow endEditing:YES];
        for (UIView *subView in self.superview.subviews) {
            if ([NSStringFromClass([subView class]) isEqualToString:@"LiuqsEmoticonKeyBoard"]) {
                if (subView.bottom == kBDScreenHeight) {
                    [UIView animateWithDuration:0.3 animations:^{
                        subView.top = kBDScreenHeight;
                    } completion:^(BOOL finished) {
                        subView.hidden = YES;
                    }];
                }
                break;
            }
        }
    }
}

@end


@implementation UIButton (receivedKeyboard)

- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
    BOOL result = [super beginTrackingWithTouch:touch withEvent:event];
    if (self.endWindowEditingWhenTouch == YES) {
        [[UIApplication sharedApplication].keyWindow endEditing:YES];
    }
    return result;
}

@end

